package com.stl;

public class NormalList {
	private Node head;
	public NormalList(Node head)
	{
		this.head = head;
		head.last=null;
		head.next=null;
	}
	public void Sort()
	{
		Node tmp =head;
		while(tmp!=null)
		{
			Node tmp_j = tmp.next;
			while(tmp_j!=null)
			{
				if(tmp.compare(tmp_j.value)>0){
					Object value = tmp.value;
					tmp.value = tmp_j.value;
					tmp_j.value = value;
				}
				tmp_j = tmp_j.next;
			}
			tmp = tmp.next;
		}
	}
	public Node getByValue(Object Value)
	{
		Node res = null;
		Node tmp =head;
		while(tmp!=null)
		{
			if(tmp.compare(Value)==0){
				res = tmp;
				break;
			}
			tmp = tmp.next;
		}
		return res;
	}
	//把节点添加到头部
	public void InsertNode(Node node)
	{
		//2 3 5 6 
		Node tmp =head;
		if(tmp.last==null){
			node.last = null;
			node.next = tmp;
			this.head = node;
			tmp.last= node;
			
		}
	
	}
	
	//删除一个节点
	public boolean DeleteNode(Object value)
	{
		Node node = this.getByValue(value);
		if(node!=null){
			if(node.last==null){
				this.head = node.next;
			}
			else if(node.next!=null){
				node.last.next = node.next;
				node.next.last = node.last;
			}
			else if(node.next==null){
				node.last.next = node.next;
			}
			node = null;
			return true;
		}
		return false;
	}
	public void print()
	{
		Node tmp =head;
		while(tmp!=null)
		{
			System.out.print(tmp.value.toString()+',');
			tmp = tmp.next;
		}
		System.out.println();
	}
}
